package util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

public class ComUtil {
	//获取用户真实的IP地址
	public static String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

	/**
	 * 获取N天日�?
	 * @return
	 */
	public static List getDateList(Integer dateNum) {
		List dateList = new ArrayList();
		Map dateMap;
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd�?");
		SimpleDateFormat sdfHasTime = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");

		Calendar c = Calendar.getInstance();
		c.setTime(date);
		String nowDate = sdf.format(date);
		String nowDateVale = sdfHasTime.format(date);
		dateMap = new HashMap();
		dateMap.put("dateValue", nowDateVale);
		dateMap.put("dateFormat", nowDate);
		dateList.add(dateMap);
		for (int i = 0; i < dateNum -1 ; i++) {
			dateMap = new HashMap();
			c.add(Calendar.DAY_OF_MONTH, 1);
			Date aa = c.getTime();
			String roomDateVale = sdfHasTime.format(aa);
			String roomDate = sdf.format(aa);
			dateMap.put("dateValue", roomDateVale);
			dateMap.put("dateFormat", roomDate);
			dateList.add(dateMap);

		}

		return dateList;
	}

	/**
	 * 获取N天以后的日期
	 * @return
	 */
	public static String getAfterDaysDate(String dateStr, Integer dateNum,
			String dateFormat) {

		SimpleDateFormat strToDate = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
		Date date = null;
		try {
			date = strToDate.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DAY_OF_MONTH, dateNum );
		date = c.getTime();
		String roomDateVale = sdf.format(date);

		return roomDateVale;
	}

	/**
	 * 获取N天以后的日期
	 * @return
	 */
	public static String getAfterHoursDate(String dateStr, Integer hour,
			String dateFormat) {
		SimpleDateFormat strToDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
		Date date = null;
		try {
			date = strToDate.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.HOUR_OF_DAY, hour);
		date = c.getTime();
		String roomDateVale = sdf.format(date);
		return roomDateVale;
	}

	public static String getFmtDate(String dateStr) {
		String speakDated = dateStr.substring(0, 10);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd�?");
		SimpleDateFormat sdfa = new SimpleDateFormat("yyyy-MM-dd");
		Date goDate = null;
		try {
			goDate = sdfa.parse(speakDated);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String goDateStr = sdf.format(goDate);
		return goDateStr;
	}

	public static String getFmtDateTM(String dateStr) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd�? HH时mm�?");
		SimpleDateFormat sdfa = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date goDate = null;
		try {
			goDate = sdfa.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String goDateStr = sdf.format(goDate);
		return goDateStr;
	}
	public static String getDateToStr(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String goDateStr = sdf.format(date);
		return goDateStr;
	}

	/**
	 * get the kind format of date
	 * @param flag
	 * @return
	 */
	public static String getNowDate(Integer flag) {
		Date date = new Date();
		String nowDate = null;
		switch (flag) {
		case 1:
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			nowDate = sdf.format(date);
			break;
		case 2:
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd�?");
			nowDate = sdf1.format(date);
			break;
		case 3:
			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd�? HH时mm�?");
			nowDate = sdf2.format(date);
			break;
		default:
			break;
		}
		return nowDate;
	}

	public static String getResourceId(String kind) {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String resourceId = kind + sdf.format(date);

		return resourceId;
	}

	public static String getResourceName(String kind) {
		String resourceName = null;
		if (kind.equals("rm")) {
			resourceName = "房间";
		} else if (kind.equals("dnrm")) {
			resourceName = "包房";
		} else if (kind.equals("dntb")) {
			resourceName = "餐桌";
		}

		return resourceName;
	}

	public static Date getNowDate() {
		Date date = new Date();
		String nowDate = null;
		Date nowTime = null;

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		nowDate = sdf.format(date);
		try {
			nowTime = sdf.parse(nowDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return nowTime;
	}
	public static Date getFmtDateTMM(String dateStr) {
		SimpleDateFormat sdfa = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date goDate = null;
		try {
			goDate = sdfa.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return goDate;
	}
	
	
	public static String getNo() {
		Calendar cal = Calendar.getInstance();
		// 此处取年后产生的随机数的冲突几率会比较大。所以考虑用别的代替
		String v_strDate = String.valueOf(cal.get(Calendar.YEAR));

		int v_intMonth = cal.get(Calendar.MONTH) + 1;
		int v_intDAY = cal.get(Calendar.DAY_OF_MONTH);
		int v_intHOUR = cal.get(Calendar.HOUR_OF_DAY);
		int v_intMINUTE = cal.get(Calendar.MINUTE);
		int v_intSECOND = cal.get(Calendar.SECOND);

		if (v_intMonth < 10) {
			v_strDate = v_strDate + "0" + v_intMonth;
		} else {
			v_strDate = v_strDate + v_intMonth;
		}
		if (v_intDAY < 10) {
			v_strDate = v_strDate + "0" + v_intDAY;
		} else {
			v_strDate = v_strDate + v_intDAY;
		}
		if (v_intHOUR < 10) {
			v_strDate = v_strDate + "0" + v_intHOUR;
		} else {
			v_strDate = v_strDate + v_intHOUR;
		}
		if (v_intMINUTE < 10) {
			v_strDate = v_strDate + "0" + v_intMINUTE;
		} else {
			v_strDate = v_strDate + v_intMINUTE;
		}
		if (v_intSECOND < 10) {
			v_strDate = v_strDate + "0" + v_intSECOND;
		} else {
			v_strDate = v_strDate + v_intSECOND;
		}

		return v_strDate;
	}
	public static String getShortNo() {

		double d = Math.random();
		String temp = Double.toString(d);
		int t = temp.indexOf(".");
		temp = temp.substring(t + 1, t + 9);
		String s = getNo().substring(0, 8) + temp;
		return s;
	}
	
	public static void main(String args[]) {
		ComUtil aa = new ComUtil();
		String bb = aa.getAfterHoursDate("2009-01-04 13:00:00", 3,
				"yyyy-MM-dd HH:mm:ss");
		//		   List bb = aa.getDateList(7);
		//		   for(int i=0;i<bb.size();i++) {
		//			   Map cc = (Map)bb.get(i);
		//			   System.out.println(cc.get("dateValue"));
		//			   System.out.println(cc.get("dateFormat"));
		//		   }
	}
}
